<?php

namespace app\common\model;

use think\Db;
use think\Model;
use app\common\model\AppraiseModel as app;
/**
 * Class HotelModel
 * @package app\common\model\Hotel
 * 酒店公共模型类
 */
class HotelModel extends Model
{
    // 模型名
    protected $name = 'HotelModel';
    // 数据库表名
    protected $table = 'lv_hotel';

    /**
     * 获取酒店列表
     */
    public function getTourList($where, $page = '', $limit = '', $filed = '')
    {
        if ($filed === '') {
            $filed = '*';
        }

        if ($page == '' && $limit == '') {
            $data = Db::name('hotel')
                ->field($filed)->where($where)
                ->find();
        } else {
            $data = Db::name('hotel')->alias('a')
                ->field('a.*,b.h_admin_id,b.h_admin_name,b.pid,b.add_time')
                ->join('hotel_admin b','a.hotel_id=b.hotel_id',"LEFT")
                ->where($where)
                ->where('b.pid',0)
                ->limit($page, $limit)
                ->order('a.hotel_id', 'desc')->select();
            //处理时间
            foreach($data as $k =>$v)
            {
                $data[$k]['examine_time'] = date('Y-m-d H:i:s',$v['examine_time']);
                $data[$k]['appraise'] = model('AppraiseModel')->getAppRate(2,$v['hotel_id']);
            }
        }

        return $data;
    }

    /**
     * 后台查询酒店总数量
     * @param $where array 条件
     * @return int|string
     */
    public function getTourCount($where)
    {
        $count =Db::name('hotel')->alias('a')
            ->field('a.*,b.h_admin_id,b.h_admin_name,b.pid,b.add_time')
            ->join('hotel_admin b','a.hotel_id=b.hotel_id',"LEFT")
            ->where($where)->where('b.pid',0)->count();
        return $count;
    }

    /**
     * 后台酒店删除
     * @param $where
     * @return int
     */
    public function  hotelDel($where)
    {
        return Db::name('hotel')->where($where)->setField('is_del',1);
    }
    /**
     * @param $data 添加导游 插入的数据
     */
    public function insertView($data)
    {
        return Db::name('hotel')->insertGetId($data);
    }
    /*
       * 修改景点密码
       */
    public function editPass($where,$data)
    {
        return Db::name('hotel_admin')->where($where)->setField('password',$data);
    }

    /*
     * 修改
     */
    public function edit($where, $data)
    {
        return DB::name('hotel')->where($where)->update($data);
    }
    /**
     * [getHotelReserveInfo 酒店预定页面接口]
     * @Author   kendy
     * @DateTime 2019-11-08T10:49:31+0800
     * @param    [type]                   $room_id [房间id]
     * @return   [type]                            [description]
     */
    public function getHotelReserveInfo($room_id)
    {
        $resultData = Db::name('hotel_room')
        ->alias('hr')
        ->field('hr.room_id,hr.room_name,hr.room_price,hr.room_discount_price,hr.available_person,hr.square,hr.is_tehui,hr.is_quxiao,hr.is_window,hr.is_wc,hr.is_food,hr.is_qian_fuwu,hr.is_stop_car,hr.is_wifi,h.should_konwn')
        ->join('hotel h','h.hotel_id=hr.hotel_id','left')
        ->where(['hr.room_id'=>$room_id,'hr.is_del'=>0,'hr.is_display'=>0,'h.is_del'=>0,'h.is_pass'=>1])
        ->find();
        if(!empty($resultData))
        { 
          $resultData['image'] = Db::name('room_img')->where('room_id',$room_id)->order('sort desc')->limit(0,1)->value('img_path');
          $resultData['image'] = addWaterByField($resultData['image']);
          if($resultData['is_tehui']=1) $resultData['room_price'] = $resultData['room_discount_price'];
          unset($resultData['room_discount_price']);
          unset($resultData['is_tehui']);
           return $resultData;
        }

        return $resultData;
    }
}